package com.metadata_system.api_server.system_server;


import com.metadata_system.api.annotations.ApiCheckSign;
import com.metadata_system.api.annotations.CheckTokenUser;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.http.request_dto.LoginUserRequest;
import com.metadata_system.system.request.ConfigListParams;
import com.metadata_system.system.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("systemConfigController")
@RequestMapping(value = "/api/system-server/v001/Config")
public class ConfigController {
    @Autowired
    ConfigService configService;
    @Autowired
    LoginUserRequest loginUserRequest;

    @RequestMapping("getList")
    @CheckTokenUser(role = {"Super","System"})
    @ApiCheckSign
    public ApiResult getList(
            @RequestParam(required = false) String name,
            @RequestParam(required = false, defaultValue = "id") String sortProp,
            @RequestParam(required = false, defaultValue = "DESC") String sortOrder,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize
    ) {
        return this.configService.getList(ConfigListParams.builder()
                .name(name)
                .sortOrder(sortOrder)
                .sortProp(sortProp)
                .page(page)
                .pageSize(pageSize)
                .build());
    }

    @RequestMapping("setKeyValue")
    @CheckTokenUser(role = {"Super","System"})
    @ApiCheckSign
    public ApiResult setKeyValue(
            @RequestParam String name,
            @RequestParam String myKey,
            @RequestParam String content
    ) {
        return this.configService.setKeyValue(myKey, name, content,this.loginUserRequest.getTokenUser().decodeID());
    }

    @RequestMapping("delete")
    @CheckTokenUser(role = {"Super","System"})
    @ApiCheckSign
    public ApiResult delete(
            @RequestParam Integer id
    ) {
        return this.configService.delete(id,this.loginUserRequest.getTokenUser().decodeID());
    }

}
